package com.ilop.sthome.mvp.present.scene;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.example.common.mvp.BasePresenterImpl;
import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.enums.DeviceTrigger;
import com.ilop.sthome.data.enums.SmartDevice;
import com.ilop.sthome.data.greenDao.AutomationBean;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.data.greenDao.SceneBean;
import com.ilop.sthome.mvp.contract.scene.SetActionContract;
import com.ilop.sthome.ui.activity.scene.AutomationDetailActivity;
import com.ilop.sthome.ui.activity.scene.ChooseActionActivity;
import com.ilop.sthome.utils.CoderUtils;
import com.ilop.sthome.utils.greenDao.utils.SceneDaoUtil;
import com.siterwell.familywellplus.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author skygge
 * @date 2020/4/14.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：输入输出条件编辑
 */
public class SetActionPresenter extends BasePresenterImpl<SetActionContract.IView> implements SetActionContract.IPresent {

    private Context mContext;

    private boolean isGateway;
    private List<String> mTrigger;
    private ArrayList<String> items_trigger;

    private DeviceInfoBean mDevice;

    public SetActionPresenter(Context mContext) {
        this.mContext = mContext;
        mTrigger = new ArrayList<>();
        items_trigger = new ArrayList<>();
    }

    @Override
    public void getDeviceInfo(DeviceInfoBean deviceInfoBean) {
        this.mDevice = deviceInfoBean;
        if (mDevice!=null){
            if ("GATEWAY".equals(mDevice.getNodeType()) || mDevice.getDevice_ID() == 0){
                String title = mDevice.getNickName()==null ? mContext.getString(R.string.ali_gateway) : mDevice.getNickName();
                mView.showTitle(title);
            }else {
                if (TextUtils.isEmpty(mDevice.getSubdeviceName())) {
                    String title = mContext.getString(SmartDevice.getType(mDevice.getDevice_type()).getTypeStrId()) + mDevice.getDevice_ID();
                    mView.showTitle(title);
                } else {
                    mView.showTitle(mDevice.getSubdeviceName());
                }
            }
        }
    }

    @Override
    public void getTrigger(boolean isLock) {
        if (!TextUtils.isEmpty(mDevice.getProductKey()) || "GATEWAY".equals(mDevice.getDevice_type())){
            isGateway = true;
            if (!isLock) {
                String[] code = mContext.getResources().getStringArray(R.array.EE_DEV_GATEWAY);
                String[] str = mContext.getResources().getStringArray(R.array.gateway_actions);
                mTrigger.addAll(Arrays.asList(code));
                items_trigger.addAll(Arrays.asList(str));
            }
            List<SceneBean> mSceneList = SceneDaoUtil.getInstance().findAllScene(mDevice.getDeviceName());
            for (SceneBean scene:mSceneList) {
                mTrigger.add("51" + CoderUtils.setCodeSupplement1Byte(Integer.toHexString(scene.getSid())));
                String switch_to = mContext.getString(R.string.switch_to);
                switch (scene.getSid()){
                    case 0:
                        items_trigger.add(switch_to + mContext.getString(R.string.home_mode));
                        break;
                    case 1:
                        items_trigger.add(switch_to + mContext.getString(R.string.out_mode));
                        break;
                    case 2:
                        items_trigger.add(switch_to + mContext.getString(R.string.sleep_mode));
                        break;
                    default:
                        items_trigger.add(switch_to + scene.getModleName());
                        break;
                }
            }
        }else {
            isGateway = false;
            String[] code;
            String[] str;
            if ("0109".equals(mDevice.getDevice_type())){
                code = mContext.getResources().getStringArray(R.array.EE_DEV_PIR_NEW);
                str = mContext.getResources().getStringArray(R.array.pir_actions_new);
            }else if ("010A".equals(mDevice.getDevice_type())){
                code = mContext.getResources().getStringArray(R.array.EE_DEV_DOOR_NEW);
                str = mContext.getResources().getStringArray(R.array.door_actions_new);
            }else {
                DeviceTrigger trigger = DeviceTrigger.getType(mDevice.getDevice_type());
                code = mContext.getResources().getStringArray(trigger.getCode());
                str = mContext.getResources().getStringArray(trigger.getState());
            }
            mTrigger.addAll(Arrays.asList(code));
            items_trigger.addAll(Arrays.asList(str));
        }
        mView.showTrigger(items_trigger);
        mView.showTriggerCode(mTrigger);
    }

    @Override
    public void getMinuteAndSecond() {
        ArrayList<String> number = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            String item = String.valueOf(i);
            if (item.length() == 1) {
                item = "0" + item;
            }
            number.add(item);
        }
        mView.showMinuteAndSecond(number);
    }

    @Override
    public void showUpdateData() {
        String status = mDevice.getDevice_status();
        if (status.length()>=6){
            int minute = Integer.parseInt(status.substring(status.length()-4, status.length()-2),16);
            int second = Integer.parseInt(status.substring(status.length()-2),16);
            mView.showTime(minute, second);
        }
        if ("GATEWAY".equals(mDevice.getDevice_type()) || mDevice.getDevice_ID() == 0){
            switch (status.substring(0, 1)) {
                case "2":
                    String code = status.substring(2, 4);
                    getTrigger(0, code);
                    break;
                case "3":
                    mView.showAction(3);
                    break;
                case "4":
                    String code1 = status.substring(2, 4);
                    getTrigger2(code1);
                    break;
                case "5":
                    String code2 = "51" + status.substring(2, 4);
                    getTrigger(7, code2);
                    break;
            }
        }else {
            if (status.length()>=6){
                String code = status.substring(0, 1) + "0";
                getTrigger(0, code);
            }else {
                getTrigger(0, status);
            }
        }
    }

    private void getTrigger(int start, String code){
        for (int i = start; i < mTrigger.size(); i++) {
            if (code.equals(mTrigger.get(i))) {
                mView.showAction(i);
                break;
            }
        }
    }

    private void getTrigger2(String code){
        for (int i = 4; i < 7; i++) {
            String code2 = mTrigger.get(i).substring(2, 4);
            if (code2.equals(code)) {
                mView.showAction(i);
                break;
            }
        }
    }

    @Override
    public void isAddNewInputAction() {
        AutomationBean mAuto = new AutomationBean();
        mAuto.setDeviceName(mDevice.getDeviceName());
        List<DeviceInfoBean> mDeviceList = new ArrayList<>();
        mDeviceList.add(mDevice);
        mAuto.setInputList(mDeviceList);
        Intent intent = new Intent();
        intent.setClass(mContext, ChooseActionActivity.class);
        intent.putExtra(CommonId.KEY_ADD_SCENE, true);
        intent.putExtra(CommonId.KEY_INPUT, false);
        intent.putExtra(CommonId.KEY_DEVICE_NAME, mDevice.getDeviceName());
        intent.putExtra(CommonId.KEY_AUTOMATION, mAuto);
        mContext.startActivity(intent);
    }

    @Override
    public void isAddNewOutputAction(AutomationBean automation) {
        List<DeviceInfoBean> mDeviceList = new ArrayList<>();
        mDeviceList.add(mDevice);
        automation.setOutputList(mDeviceList);
        Intent intent = new Intent();
        intent.setClass(mContext, AutomationDetailActivity.class);
        intent.putExtra(CommonId.KEY_ADD_SCENE, true);
        intent.putExtra(CommonId.KEY_AUTOMATION, automation);
        intent.putExtra(CommonId.KEY_DEVICE_NAME, mDevice.getDeviceName());
        mContext.startActivity(intent);
    }

    @Override
    public void editInputDeviceInfo(String action) {
        mDevice.setDevice_status(action);
    }

    @Override
    public void editOutputDeviceInfo(int minute, int second, String action) {
        if (minute != 0 || second != 0) {
            String delay = CoderUtils.setCodeSupplement1Byte(Integer.toHexString(minute)) +
                    CoderUtils.setCodeSupplement1Byte(Integer.toHexString(second));
            String actionCode;
            if (isGateway){
                actionCode = CoderUtils.getGatewayActionCode(action, delay, true);
            }else {
                actionCode = CoderUtils.getSubDeviceActionCode(action, delay, true);
            }
            mDevice.setDevice_status(actionCode);
            mDevice.setBinVersion(delay);
        }else {
            String actionCode;
            if (isGateway){
                actionCode = CoderUtils.getGatewayActionCode(action, null, false);
            }else {
                actionCode = CoderUtils.getSubDeviceActionCode(action, null, false);
            }
            mDevice.setDevice_status(actionCode);
        }
    }

    @Override
    public void isAddOrUpdateInput(boolean update) {
        if (!update) {
            LiveDataBus.get().with(CommonId.INPUT_CONDITION).setValue(mDevice);
        } else {
            LiveDataBus.get().with(CommonId.INPUT_UPDATE).setValue(mDevice);
        }
    }

    @Override
    public void isAddOrUpdateOutput(boolean update) {
        if (!update) {
            LiveDataBus.get().with(CommonId.OUTPUT_CONDITION).setValue(mDevice);
        } else {
            LiveDataBus.get().with(CommonId.OUTPUT_UPDATE).setValue(mDevice);
        }
    }


}
